package com.forum.demo.services.impl;

import ch.qos.logback.core.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forum.demo.common.ResultCode;
import com.forum.demo.mapper.MessageMapper;
import com.forum.demo.model.Message;
import com.forum.demo.model.UserInfo;
import com.forum.demo.model.response.MessageResponse;
import com.forum.demo.services.IMessageService;
import com.forum.demo.utils.checkFailUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private UserServiceImpl userService;
    @Resource
    private MessageMapper messageMapper;
    @Override
    public void create(Message message) {
        if (message == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (message.getPostUserId() == null || message.getReceiveUserId() == null
                || StringUtil.isNullOrEmpty(message.getContent())) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        UserInfo user = userService.selectById(message.getReceiveUserId());
        if (user == null) {
            // 抛出异常
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 设置默认值
        message.setState((byte) 0); // 表示未读状态
        message.setDeleteState((byte) 0);
        // 设置创建与更新时间
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int row = messageMapper.insert(message);
        if (row != 1) {
            checkFailUtils.checkFail(ResultCode.FAILED_CREATE);
        }

    }

    @Override
    public Message selectById(Long id) {
        if (id == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Message message = messageMapper.selectOne(new LambdaQueryWrapper<Message>().eq(Message::getId, id));
        return message;
    }

    @Override
    public Long selectUnreadCount(Long receiveUserId) {
        if (receiveUserId == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        UserInfo user = userService.selectById(receiveUserId);
        if (user == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Long num = messageMapper.selectCount(new LambdaQueryWrapper<Message>()
                .eq(Message::getState,0)
                .eq(Message::getDeleteState,0)
                .eq(Message::getReceiveUserId,receiveUserId));
        return num;
    }

    @Override
    public List<MessageResponse> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId == null || receiveUserId <= 0) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        List<MessageResponse> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if (id == null || state < 0 || state > 2) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Message message = new Message();
        message.setId(id);
        message.setState(state);
        message.setUpdateTime(new Date());
        int row = messageMapper.update(message, new LambdaQueryWrapper<Message>().eq(Message::getId, id));
        if (row != 1) {
            checkFailUtils.checkFail(ResultCode.FAILED_CREATE);
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId == null || StringUtil.isNullOrEmpty(message.getContent())) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Message existsMessage = messageMapper.selectOne(new LambdaQueryWrapper<Message>().eq(Message::getId, repliedId));
        if (existsMessage == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        updateStateById(repliedId, (byte) 2);
        create(message);
    }
}
